package cn.edu.zhku.citrusmonitor.illness.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.edu.zhku.citrusmonitor.domain.TbIllness;
import cn.edu.zhku.citrusmonitor.domain.TbUser;
import cn.edu.zhku.citrusmonitor.illness.service.IllnessService;
import cn.edu.zhku.citrusmonitor.illness.service.IllnessServiceImpl;
import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.xy.PageBean;
import net.sf.json.JSONObject;

/**
 * 
 * @author 欣裕
 * @date 2018年8月2日
 * 
 */
@WebServlet("/illness.do")
public class IllnessServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;

	public JSONObject add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IllegalAccessException, InvocationTargetException, ParseException {
		TbIllness illness = new TbIllness();
		IllnessService IS = new IllnessServiceImpl();
		TbUser user = (TbUser) request.getSession().getAttribute("user");
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		BeanUtils.populate(illness, request.getParameterMap());		
		return IS.addIllness(illness,user);
	}
	
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IllegalAccessException, InvocationTargetException {
			TbIllness illness = new TbIllness();
			IllnessService IS = new IllnessServiceImpl();
			TbUser user = (TbUser) request.getSession().getAttribute("user");
			BeanUtils.populate(illness, request.getParameterMap());
			System.out.println(illness.getIllnessId());
			return IS.modifyIllness(illness,user);
	}
	
	public JSONObject remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IllegalAccessException, InvocationTargetException {
		TbIllness illness = new TbIllness();
		IllnessService IS = new IllnessServiceImpl();
		TbUser user = (TbUser) request.getSession().getAttribute("user");
		BeanUtils.populate(illness, request.getParameterMap());
		return IS.removeIllness(illness,user);
	}
	
	/**
	 * 显示病情详细记录,后台管理
	 * @param request
	 * @param response
	 * @return JSON.toJSONString(IS.getIllness(id))
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findIllnessByIllnessId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		IllnessService IS = new IllnessServiceImpl();
		String id = request.getParameter("illnessId");
		return JSON.toJSONStringWithDateFormat(IS.getIllness(id), "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 分页查看查看病情列表
	 * @param request
	 * @param response
	 * @return JsonUtil.object2json(pbn)
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public String findAll(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		IllnessService IS = new IllnessServiceImpl();
		TbIllness illness = new TbIllness();
		PageBean<TbIllness> pbn = new PageBean<TbIllness>();
		BeanUtils.populate(illness, request.getParameterMap());
		BeanUtils.populate(pbn, request.getParameterMap());
		pbn = IS.findIllnessPageBean(pbn.getCurrentPage(), pbn.getPageSize(), illness);
		return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 
	 * 判断是否为超级用户
	 * @param request
	 * @param response
	 * @return
	 */
	public boolean isSuUser(HttpServletRequest request,HttpServletResponse response) {
		IllnessService IS = new IllnessServiceImpl();
		TbUser user = (TbUser) request.getSession().getAttribute("user");
		return IS.isVipUser(user);
	}
	
	/**
	 * 分写显示全部虫害列表(后台显示，更多信息)
	 * @param request
	 * @param response
	 * @return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY) 规定pageSize（默认10条）的一页虫害列表
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public String findAllIllnessDetail(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		IllnessService IS = new IllnessServiceImpl();
		TbIllness illness = new TbIllness();
		PageBean<TbIllness> pbn = new PageBean<TbIllness>();
		BeanUtils.populate(illness, request.getParameterMap());
		BeanUtils.populate(pbn, request.getParameterMap());	//默认一页十条
		pbn = IS.findAllIllnessDetail(pbn.getCurrentPage(), pbn.getPageSize());
		return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 搜索查询，分页显示
	 * @param request
	 * @param response
	 * @return 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public String searchIllness(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, InvocationTargetException {
		IllnessService IS = new IllnessServiceImpl();
		TbIllness illness = new TbIllness();
		PageBean<TbIllness> pbn = new PageBean<TbIllness>();
		BeanUtils.populate(illness, request.getParameterMap());
		BeanUtils.populate(pbn, request.getParameterMap());	//默认一页十条
		String word = request.getParameter("word");
		Integer pageSize = pbn.getPageSize();
		Integer currentPage = pbn.getCurrentPage();
		pbn = IS.findAllIllnessDetailByWord(currentPage, pageSize, word);
		return JSON.toJSONStringWithDateFormat(pbn, "yyyy-MM-dd", SerializerFeature.EMPTY);
	}
	
	/**
	 * 批量删除病情记录
	 * @param request
	 * @param response
	 * @return 
	 */
	public JSONObject deleteInBatch(HttpServletRequest request,HttpServletResponse response) {
		// 1.得到所有要删除的虫害id
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		// 2.调用service，完成批量删除
		IllnessService IS = new IllnessServiceImpl(); 
		System.out.println("IS.delSelect(ids)");
		return IS.delSelect(ids);
	}
}
